package com.ang.jdk8.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ssang
 * @date 2021-08-26 15:31
 */
public class FilterList {

    public static void main(String[] args) {
        FilterList filter = new FilterList();
//        filter.test01();
//        filter.test02();
        filter.test03();
    }

    //***********************************************************
    //根据多个条件过滤List，返回过滤后的List
    //***********************************************************
    public void test01(){
        List<String> oldList = Arrays.asList(new String[]{"a1","a2","b1","b2","c1","c2"});

        String[] condition = new String[]{"a1","c1"};

        //开始过滤
        List resultList = oldList.stream().filter(
                o -> this.filterListCondition(o,condition)
        ).collect(Collectors.toList());

        resultList.forEach(l -> System.out.println(l));
    }

    //筛选过滤的方法
    public boolean filterListCondition(String s,String[] arr){
        for(String rs : arr){
            if(s.equals(rs)){
                return true;
            }
        }
        return false;
    }

    //***********************************************************
    //过滤List，返回其中的对象
    //***********************************************************
    public void test02(){
        User u1 = new User("1","test1",21);
        User u2 = new User("2","test2",22);
        User u3 = new User("3","test3",23);

        List<User> list = new ArrayList<>();
        list.add(u1);
        list.add(u2);
        list.add(u3);

        //开始过滤
        User u = list.stream().filter(o -> o.id.equals("5"))
                .findAny()
                .orElse(new User("4","test4",24));

        System.out.println(u.toString());
    }

    //***********************************************************
    //过滤List，返回其中的对象（对象可能为null，用optional封装）
    //***********************************************************
    public void test03(){
        User u1 = new User("1","test1",21);
        User u2 = new User("2","test2",22);
        User u3 = new User("3","test3",23);

        List<User> list = new ArrayList<>();
        list.add(u1);
        list.add(u2);
        list.add(u3);

        //开始过滤
        Optional<User> optionalUser = list.stream().filter(o -> o.id.equals("5"))
                .findAny();

        String name = optionalUser.map(o->o.name).orElse("-");
        System.out.println(String.format("筛选id为5的name为：%s",name));
    }

    //***********************************************************
    //简单的实体类
    //***********************************************************
    class User{
        String id;
        String name;
        int age;

        public User(String id,String name,int age){
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public String toString(){
            return this.id +";"+ this.name +";"+ this.age;
        }
    }
}
